<!DOCTYPE html>
<html>

<head>
  <title>猜电影</title>
  <link rel="stylesheet" href="/stylesheets/weui.css">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,minimum-scale=1">
  <style>
    .open {
      background-color: antiquewhite;
    }

  </style>
</head>

<body>
  <h1 id="recorder">点击标题，开始录音</h1>
  <a href="javascript:;" class="weui-btn weui-btn_primary">weui按钮样式</a>
  <a href="javascript:;" class="weui-btn weui-btn_disabled weui-btn_primary">weui按钮disabled样式</a>

  <p id="title"></p>
  <div id="director"></div>
  <div id="poster"></div>
  <div class="open">
    <wx-open-launch-weapp id="launch-btn" username="gh_c81e8d0053c0" path="pages/home/index.html">
    <button class="btn">打开小程序</button>
    </wx-open-launch-weapp>



  </div>
  <script src="/javascripts/zepto-docs.min.js"></script>
  <script src="https://res2.wx.qq.com/open/js/jweixin-1.6.0.js"></script>

</body>
<script>
  wx.config({
    debug: false,
    appId: 'wx8adf122f1d98838b',
    timestamp: '<%= timestamp %>',
    nonceStr: '<%= noncestr %>',
    signature: '<%= signature %>',
    jsApiList: [
      'startRecord',
      'stopRecord',
      'onVoiceRecord',
      'translateVoice',
      'onMenuShareTimeline',
      'onMenuShareAppMessage',
      'onMenuShareQQ',
      'onMenuShareWeibo',
      'onMenuShareQZone',
    ],
    openTagList: ['wx-open-launch-app']
  })

  wx.ready(function () {
    var btn = document.getElementById('launch-btn');
    btn.addEventListener('launch', function (e) {
      console.log('success');
    });
    btn.addEventListener('error', function (e) {
      console.log('fail', e.detail);
    });
    wx.checkJsApi({
      jsApiList: ['onMenuShareTimeline', 'onMenuShareAppMessage'],
      success: function (res) {
        console.log(res);
      }
    })
    var isRecording = false;
    var shareContent = {
      title: '敏捷互联，根据语音找电影',
      desc: '微信公众号后台开发之分享功能 ',
      link: 'http://minjiehulian.tunnel.2bdata.com/movie',
      imgUrl: 'http://img.1985t.com/uploads/attaches/2012/04/5251-Du1x67.jpg',
      type: 'link',
      dataUrl: '',
      success: function () {
        window.alert('分享成功');
      },
      cancel: function () {
        window.alert('取消分享');
      }
    };
    wx.onMenuShareAppMessage(shareContent);
    var slides
    $('#poster').on('tap', function () {
      wx.previewImage(slides);
    })
    $('h1').on('tap', function () {
      if (!isRecording) {
        isRecording = true;
        $('#recorder').text('正在录音');
        wx.startRecord({
          cancel: function () {
            window.alert('请允许应用访问您的麦克风');
          }
        })
        return
      }
      isRecording = false;
      $('#recorder').text('查找电影');


      wx.stopRecord({
        success: function (res) {
          var localId = res.localId
          wx.translateVoice({
            localId: localId,
            isShowProgressTips: true,
            success: function (res) {
              var result = res.translateResult;
              window.alert(res.translateResult)
              $.ajax({
                type: 'get',
                url: 'https://api.douban.com/v2/movie/search?q=' + result,
                dataType: 'jsonp',
                jsonp: 'callback',
                success: function (data) {
                  var subject = data.subjects[0];
                  $('#director').html(subject.directors[0].name)
                  $('#poster').html('<img src=" ' + subject.images.large + ' ">')

                  //分享内容
                  shareContent = {
                    title: subject.title,
                    desc: '我找到了' + subject.title,
                    link: location.href,
                    imgUrl: subject.images.large,
                    type: 'link',
                    dataUrl: '',
                    success: function () {
                      window.alert('分享成功');
                    },
                    cancel: function () {
                      window.alert('取消分享');
                    }
                  }
                  wx.onMenuShareAppMessage(shareContent);
                  slides = {
                    current: subject.images.large,
                    urls: [subject.images.large]
                  }
                  data.subjects.forEach(function (item) {
                    slides.urls.push(item.images.large);
                  })
                }
              })
            }
          })
        }
      })
    })
  })

</script>

</html>
